home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 10 - 1994 / 10.09 Sep 94 / Getting Started / Various bits < prev   
Encoding:
Text File  |  1994-08-19  |  2.0 KB  |  86 lines  |  [TEXT/R*ch]

  1. Various bits of source from the Getting Started September article
  2.  
  3.  
  4.  
  5. The CPushButton class definition might look something like this:
  6.  
  7. class CPushButton
  8. {
  9.     protected:
  10.         Rect                bounds;
  11.         Str255            title;
  12.         Boolean            isDefault;
  13.         WindowPtr        owningWindow;
  14.         ControlHandle    buttonControl;
  15.  
  16.     public:
  17.                             CPushButton( WindowPtr owningWindow, Rect *bounds,
  18.                                 Str255 title, Boolean isDefault );
  19.         virtual            ~CPushButton();
  20.         virtual void    Draw();
  21.         virtual void    DoClick();
  22. };
  23. Here’s my definition of a CPictureButton class:
  24.  
  25. class CPictureButton : public CPushButton
  26. {
  27.     protected:
  28.         PicHandle        pic;
  29.  
  30.     public:
  31.                             CPictureButton( WindowPtr owningWindow, Rect *bounds,
  32.                                 PicHandle pic, Boolean isDefault );
  33.         virtual            ~CPushButton();
  34.         virtual void    Draw();
  35.         virtual void    DoClick();
  36. };
  37.  
  38.  
  39. Here’s a CPictureButton definition:
  40.  
  41. CPictureButton        *myPictureButton;
  42.  
  43. myPictureButton = new CPictureButton( myWindow,
  44.                             &buttonRect, myPicture, true );
  45.  
  46. When you create a derived class object, you provide the parameters you want
  47. passed to the derived classes’ constructor:
  48.  
  49. CPictureButton        *myPictureButton;
  50.  
  51. myPictureButton = new CPictureButton( myWindow,
  52.                             &buttonRect, myPicture, true );
  53.  
  54. The format of the base classes’ constructor call are specified in the derived
  55. constructor’s title line. For example:
  56.  
  57. CPictureButton::CPictureButton( WindowPtr owningWindow, Rect *bounds,
  58.                                 PicHandle pic, Boolean isDefault )
  59.             : CPushButton( owningWindow, bounds, "\p", isDefault );
  60.  
  61. I promised an explanation of the mysterious virtual keyword. 
  62. class CPictureButton : public CPushButton
  63. {
  64.     protected:
  65.         PicHandle        pic;
  66.  
  67.     public:
  68.                             CPictureButton( WindowPtr owningWindow, Rect *bounds,
  69.                                 PicHandle pic, Boolean isDefault );
  70.         virtual            ~CPushButton();
  71.         virtual void    Draw();
  72.         virtual void    DoClick();
  73. };
  74.  
  75. Now suppose you define a CMyPictureButton object, like this:
  76.  
  77. CPictureButton        *buttonPtr;
  78.  
  79. buttonPtr = new CMyPictureButton( /* put params here */ );
  80.  
  81. To call CPictureButton’s version of Draw(), call:
  82.  
  83. CPictureButton::Draw()
  84.  
  85.  
  86.